﻿using TbShop.Business.Components;
using TbShop.Entity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TbShop.Web.Helpers;

namespace TbShop.Web.Areas.Admin.Controllers
{
    public class OperationLogController : BaseController
    {
        private OperationLogBC logBC = new OperationLogBC();
        /// <summary>
        /// 日志管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.OperationType = DropdownHelper.ConvertToSelectListItem(Constants.CodeCategories.OperationType).Select(a => new SelectListItem
            {
                Text = a.Text,
                Value = a.Text
            });
            ViewBag.UserName = JsonConvert.SerializeObject(AdminHelper.GetAllUsers().Select(a => new { Id = a.Id, UserName = a.UserName, Name = a.Name }));
            return View();
        }
        /// <summary>
        /// 日志列表
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        public string GetLogList(int pageSize = 0, int pageNumber = 0)
        {
            var begin = Request.QueryString["begin"];
            var end = Request.QueryString["end"];
            var ope = Request.QueryString["operation"];
            var userName = Request.QueryString["username"];
            List<string> userIds = null;
            if (!string.IsNullOrWhiteSpace(userName))
            {
                userIds = new List<string>();
                var findUser = AdminHelper.GetAllUsers().Where(x => x.Name.Contains(userName));
                if (findUser.Any())
                {
                    userIds = findUser.Select(a => a.UserName).ToList();
                }
            }
            var data = logBC.GetPagedListBySearch(begin, end, ope, userIds, pageNumber, pageSize);
            var meta = data.GetMetaData();
            var counts = meta.TotalItemCount;
            return JsonConvert.SerializeObject(new { total = counts, rows = data });
        }
        /// <summary>
        /// 清空日志
        /// </summary>
        /// <param name="begin">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="operation">操作类型</param>
        /// <param name="username">操作人</param>
        /// <returns></returns>
        [HttpPost]
        public int DeleteData()
        {
            var begin = Request["begin"];
            var end = Request["end"];
            var ope = Request["operation"];
            var userName = Request["username"];
            List<string> userIds = null;
            if (!string.IsNullOrWhiteSpace(userName))
            {
                userIds = new List<string>();
                var findUser = AdminHelper.GetAllUsers().Where(x => x.Name.Contains(userName));
                if (findUser.Any())
                {
                    userIds = findUser.Select(a => a.UserName).ToList();
                }
            }
            var data = logBC.GetPagedListBySearch(begin, end, ope, userIds, 1, int.MaxValue);
            if (data.Any())
            {
                logBC.RemoveAll(data);
                SendMessage("删除成功", Constants.MessageType.Success);
                return 1;
            }
            else
            {
                SendMessage("没有找到相应的数据", Constants.MessageType.Warning);
                return 0;
            }

        }
    }
}